package com.example.back.mapper;

import com.example.back.entity.Post;
import com.example.back.util.StringUtil;

public class PostUpdateProvider {
    public String getUpdatePostSql(Post post) {
        StringBuffer sql = new StringBuffer("update post set id = #{id} ");
        if(StringUtil.isNotEmpty(post.getTitle())){
            sql.append(" ,title = #{title} ");
        }
        if(StringUtil.isNotEmpty(post.getText())){
            sql.append(" ,text = #{text} ");
        }
        if(StringUtil.isNotEmpty(post.getTime())){
            sql.append(" ,time = #{time} ");
        }
        if(StringUtil.isNotEmpty(post.getUrl())){
            sql.append(" ,url = #{url} ");
        }

        sql.append(" where id = #{id}");
        return sql.toString();
    }
    public String getQueryPostSql(Post post) {
        //仅支持用户昵称和注册时间
        StringBuffer sql = new StringBuffer("SELECT * FROM post WHERE 1=1");
        if (StringUtil.isNotEmpty(post.getTitle())) {
            sql.append(" AND text LIKE concat('%',#{title},'%')");
        }
        if (StringUtil.isNotEmpty(post.getText())) {
            sql.append(" AND text LIKE concat('%',#{text},'%')");
        }
        if (StringUtil.isNotEmpty(post.getTime())) {
            sql.append(" AND time LIKE concat('%',#{time},'%')");
        }
        if (post.getUserId()!= null){
            sql.append(" AND userId = #{userId}");
        }
        return sql.toString();
    }
}
